#include "settingsdialog.h"
#include <QLabel>

SettingsDialog::SettingsDialog(QWidget *parent)
    : QDialog(parent),
    intervalSpinBox(new QSpinBox(this)),
    unitComboBox(new QComboBox(this))
{
    setWindowTitle("设置自动备份间隔");

    // 设置间隔SpinBox的范围
    intervalSpinBox->setRange(1, 999); // 根据需要调整范围
    intervalSpinBox->setValue(1);      // 默认值为1

    // 设置单位ComboBox的选项
    QStringList units;
    units << "秒" << "分钟" << "小时"; // 可以根据需要添加更多单位
    unitComboBox->addItems(units);
    unitComboBox->setCurrentIndex(0); // 默认单位为分钟

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(new QLabel("备份间隔:", this));
    layout->addWidget(intervalSpinBox);
    layout->addWidget(new QLabel("单位:", this));
    layout->addWidget(unitComboBox);

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    QPushButton *acceptButton = new QPushButton("确定", this);
    QPushButton *rejectButton = new QPushButton("取消", this);
    buttonLayout->addWidget(acceptButton);
    buttonLayout->addWidget(rejectButton);
    layout->addLayout(buttonLayout);

    connect(acceptButton, &QPushButton::clicked, this, &SettingsDialog::onAccepted);
    connect(rejectButton, &QPushButton::clicked, this, &SettingsDialog::onRejected);
}

SettingsDialog::~SettingsDialog()
{
    // 无需显式删除intervalSpinBox和unitComboBox，因为它们作为子对象会被自动删除
}

int SettingsDialog::getBackupInterval() const
{
    return intervalSpinBox->value();
}

QString SettingsDialog::getBackupUnit() const
{
    return unitComboBox->currentText();
}

void SettingsDialog::onAccepted()
{
    // 在这里添加保存设置的逻辑
    // 例如，将间隔和单位保存到配置文件或应用设置中
    accept(); // 关闭对话框并返回QDialog::Accepted
}

void SettingsDialog::onRejected()
{
    reject(); // 关闭对话框并返回QDialog::Rejected
}
